package com.teichmann.core.util;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.htmlparser.Tag;

import com.teichmann.bo.Car;
import com.teichmann.core.annotations.Delimiter;

public class CrawlerUtils {

	public static boolean containsDelimiter(Tag tag, Class clazz) {

		if (((Delimiter) clazz.getAnnotation(Delimiter.class)).tag().equalsIgnoreCase(
				tag.getTagName())
				&& (((Delimiter) clazz.getAnnotation(Delimiter.class)).attribute()) != null
				&& tag.getAttribute(
						((Delimiter) clazz.getAnnotation(Delimiter.class)).attribute())
						.contains(((Delimiter) clazz.getAnnotation(Delimiter.class)).value())) {

			return true;
		}
		return false;
	}

	public static String removeUnusedFields(String string) {
		String adjusted = null;
		adjusted = string.replaceAll("\n", "");
		adjusted = string.replaceAll("\r", "");
		adjusted = string.replaceAll("\t", "");
		adjusted = string.replaceAll("&nbsp;", "");

		return adjusted.trim();
	}

	public static List<Car> sort(List<Car> cars) {
		Collections.sort(cars, new Comparator<Car>() {
			public int compare(Car o1, Car o2) {
				Car p1 = (Car) o1;
				Car p2 = (Car) o2;
				return p1.getAnoModelo() > p2.getAnoModelo() ? -1 : (p1.getAnoModelo() < p2
						.getAnoModelo() ? +1 : 0);
			}
		});
		
		return cars;
	}
}
